﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.Framework.Server.Base;
using ZhimeiSoft.CharmManager.Server.DAL.Interface;
using System.Data.Common;

namespace ZhimeiSoft.CharmManager.Server.DAL
{
    public class ServiceItemDAL : BaseDal, IServiceItemDAL
    {
        /// <summary>
        /// 设置SQL语句中的参数
        /// </summary>
        /// <param name="cmd">DbCommand对象</param>
        /// <param name="customer">客户实体类</param>
        private void AddInSqlParameters(ref DbCommand cmd, DBEntityServiceItem serviceItem)
        {
            if (serviceItem == null || cmd == null)
                return;

            DB.AddInParameter(cmd, DBEntityServiceItem.FieldName.SNAME, DbType.String, serviceItem.SNAME);
            DB.AddInParameter(cmd, DBEntityServiceItem.FieldName.SCATEGORYID, DbType.Int32, serviceItem.SCATEGORYID);
            DB.AddInParameter(cmd, DBEntityServiceItem.FieldName.SPRICE, DbType.Decimal, serviceItem.SPRICE);
            DB.AddInParameter(cmd, DBEntityServiceItem.FieldName.STIMESPAN, DbType.Int32, serviceItem.STIMESPAN);
            DB.AddInParameter(cmd, DBEntityServiceItem.FieldName.STATE, DbType.String, serviceItem.STATE);
            DB.AddInParameter(cmd, DBEntityServiceItem.FieldName.SCALLBACKFLAG, DbType.Int32, serviceItem.SCALLBACKFLAG);
            DB.AddInParameter(cmd, DBEntityServiceItem.FieldName.SCALLBACKDAY, DbType.Int32, serviceItem.SCALLBACKDAY);
            DB.AddInParameter(cmd, DBEntityServiceItem.FieldName.STARTDATE, DbType.DateTime, serviceItem.STARTDATE);
            DB.AddInParameter(cmd, DBEntityServiceItem.FieldName.SENDDATE, DbType.DateTime, serviceItem.SENDDATE);
            DB.AddInParameter(cmd, DBEntityServiceItem.FieldName.SREMARK, DbType.String, serviceItem.SREMARK);
            DB.AddInParameter(cmd, DBEntityServiceItem.FieldName.SAVAILABLEDAYS, DbType.Int32, serviceItem.SAVAILABLEDAYS);
            DB.AddInParameter(cmd, DBEntityServiceItem.FieldName.SINDEXCODE, DbType.String, serviceItem.SINDEXCODE);
            DB.AddInParameter(cmd, DBEntityServiceItem.FieldName.SERVICEITEMCODE, DbType.String, serviceItem.SERVICEITEMCODE);
            DB.AddInParameter(cmd, DBEntityServiceItem.FieldName.SCOUNT, DbType.Int32, serviceItem.SCOUNT);
        }

        public void InsertServiceItem(DBEntityServiceItem serviceItem)
        {
            string strSQL = ServiceItemDAL_Const.SQL_INSERT_SERVICEITEM;
            DbCommand cmd = DB.GetSqlStringCommand(strSQL);
            DB.AddInParameter(cmd, DBEntityServiceItem.FieldName.SERVICEITEMID, DbType.Int32, serviceItem.SERVICEITEMID);
            AddInSqlParameters(ref cmd, serviceItem);
            DB.ExecuteNonQuery(cmd);
        }

        public void UpdateServiceItem(DBEntityServiceItem serviceItem)
        {
            string strSQL = ServiceItemDAL_Const.SQL_UPDATE_SERVICEITEM;
            DbCommand cmd = DB.GetSqlStringCommand(strSQL);
            DB.AddInParameter(cmd, DBEntityServiceItem.FieldName.SERVICEITEMID, DbType.Int32, serviceItem.SERVICEITEMID);
            AddInSqlParameters(ref cmd, serviceItem);
            DB.ExecuteNonQuery(cmd);
        }

        public DataSet QueryServiceItem(string sFilter)
        {
            string strSQL = ServiceItemDAL_Const.SQL_SELECT_SERVICEITEM;
            if (sFilter.Trim() != string.Empty)
                strSQL += " WHERE " + sFilter;
            strSQL += " ORDER BY " + DBEntityServiceItem.FieldName.STATE;
            DbCommand cmd = DB.GetSqlStringCommand(strSQL);
            return DB.ExecuteDataSet(cmd);
        }

        public void ResetServiceItemCategory(int categoryid, int newcategoryid)
        {
            string strSQL = ServiceItemDAL_Const.SQL_UPDATE_RESET_SERVICEITEM_CATEGORY;
            DbCommand cmd = DB.GetSqlStringCommand(strSQL);
            DB.AddInParameter(cmd, DBEntityServiceItem.FieldName.SCATEGORYID, DbType.Int32, categoryid);
            DB.AddInParameter(cmd, "NEW" + DBEntityServiceItem.FieldName.SCATEGORYID, DbType.Int32, newcategoryid);
            DB.ExecuteNonQuery(cmd);
        }

        public void SetServiceItemState(int serviceItemID, string sState)
        {
            string strSQL = ServiceItemDAL_Const.SQL_UPDATE_SETSTATE_SERVICEITEM;
            DbCommand cmd = DB.GetSqlStringCommand(strSQL);
            DB.AddInParameter(cmd, DBEntityServiceItem.FieldName.STATE, DbType.String, sState);
            DB.AddInParameter(cmd, DBEntityServiceItem.FieldName.SERVICEITEMID, DbType.Int32, serviceItemID);
            DB.ExecuteNonQuery(cmd);
        }
    }
}
